Face Recognition Using ‘face_recognition’ API

The world’s simplest facial recognition API for Python and the command line

In this article, we’ll explore the ‘face_recognition’ library and implement a face recognition system.

Outline:

  1. Step 1: Installation
  1. Step 2: Finding Faces in Image
  1. Step 3: Identifying Facial Features
  1. Step 4: Identifying Known Faces

Step 1: Installation:

Requirements

For windows, First make sure that you have dlib installed. If you don’t have dlib installed. Follow these steps

git clone https://github.com/davisking/dlib.git
cd ..
python3 setup.py install

After this step, you should be able to run import dlib successfully.

After successful installation of dlib library, install face_recognition using this pip command
pip3 install face_recognition

Now, you are ready to implement a real-time face recognition system.

Step 2: Finding Faces in Image:

we’ll start by importing face_recognition library

import face_recognition

Using the built-in function load_image_file() to load the image. And then using face_locations() which gives an array listing the co-ordinates of each face!

image = face_recognition.load_image_file("picture.jpg")
face_locations = face_recognition.face_locations(image)

The output will be something like this!

I found 1 face(s) in this photograph.
A face is located at pixel location Top: 241, Left: 419, Bottom: 562, Right: 740

Step 3: Identifying Facial Features:

The face_landmarks() function locates all the landmarks in a face and returns a Dictionary containing the landmarks.

# Find all facial features in all the faces in a image face_landmarks_list = face_recognition.face_landmarks(image)
print(face_landmarks_list)

And the output will be something like this!

[{'chin': [(429, 328), (426, 368), (424, 408), (425, 447), (437, 484), (460, 515), (490, 538), (524, 556), (562, 564), (600, 566), (630, 554), (655, 533), (672, 507), (684, 476), (694, 445), (702, 413), (707, 382)], 
'left_eyebrow': [(488, 294), (509, 279), (535, 278), (561, 283), (584, 296)], 
'right_eyebrow': [(622, 307), (646, 305), (670, 309), (691, 321), (698, 344)], 
'nose_bridge': [(601, 328), (599, 352), (598, 375), (596, 400)], 'nose_tip': [(555, 414), (570, 421), (586, 428), (601, 428), (614, 426)], 
'left_eye': [(512, 320), (528, 316), (544, 319), (557, 331), (541, 330), (525, 327)], 
'right_eye': [(629, 348), (647, 342), (661, 346), (672, 357), (659, 358), (644, 354)], 
'top_lip': [(519, 459), (545, 455), (566, 456), (580, 462), (595, 462), (610, 470), (627, 480), (620, 477), (593, 470), (579, 468), (564, 463), (527, 459)], 
'bottom_lip': [(627, 480), (606, 482), (589, 479), (575, 477), (560, 473), (540, 468), (519, 459), (527, 459), (563, 461), (577, 466), (592, 468), (620, 477)]}]

Step 4: Identifying Known Faces:

First, we need pictures of people we want to recognize.

List of Known People

Now, we’ll use face_encodings() function which returns a list of results of identified faces. Note that we will use [0] to grab the first person’s Face Encoding’s.

kohli = face_recognition.load_image_file("Virat Kohli.jpg")
kohli_face_encoding = face_recognition.face_encodings(kohli)[0]
ronaldo = face_recognition.load_image_file("Ronaldo.jpg")
ronaldo_face_encoding= face_recognition.face_encodings(ronaldo)[0]

Now, Create arrays of known face encodings and their names

known_face_encodings = [kohli_face_encoding, ronaldo_face_encoding]
known_face_names = ["Virat Kohli" , "Ronaldo"]
Now, To recognize a face from the Unknown Image. We should load an unknown image and Find face locations and face encodings of the unknown image.
unknown_image = face_recognition.load_image_file("Unknown.jpg")
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

Converting the image into a PIL-format image. So that we can draw bounding boxes on the image.

from PIL import Image, ImageDraw
pil_image = Image.fromarray(unknown_image)
# Create a Pillow ImageDraw Draw instance to draw
draw = ImageDraw.Draw(pil_image)

Now, Loop through each face in the Image and draw bounding Boxes:

for (t, r, b, l), in zip(face_locations, face_encodings):
  matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
# Instead, use face_distance to calculate similarities
    face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  best_match_index = np.argmin(face_distances)
  name = "Unknown"
  if matches[best_match_index]:
    name = known_face_names[best_match_index]
# Draw a box around the face using the Pillow module
  draw.rectangle(((l, t), (r, b)), outline=(0, 0, 255))
# Draw a label with a name below the face 
  width, height = draw.textsize(name)
  draw.rectangle(((l, b - height - 10), (r, b)), fill=(0, 0, 255),     outline=(0, 0, 255))
  draw.text((l + 6, b - height - 5), name, fill=(255, 255, 255, 255))
display(pil_image)
Identified and Unidentified Faces

That’s it!!!

I hope this gives you a more visual, hands on, insight into how to use face_recognition API.

Reference: